from PyQt5.QtWidgets import *
from myutil import globalvar as gl, logs
from myutil.api.ac_xk_api import submit_xk_info_from_free_course_list_element
from ui.free_select_course_form import Ui_Frame as free_select_courseFrame


class free_select_course_frame(QFrame, free_select_courseFrame):
    def __init__(self, parent=None):
        super(free_select_course_frame, self).__init__(parent)
        self.xk_kch_list = []
        self.yjxdk = []
        self.setupUi(self)

        self.xk_kxh_ok_pushButton.setEnabled(False)
        self.xk_skls_ok_pushButton.setEnabled(False)
        self.xk_ok_pushButton.setEnabled(False)
        self.xk_kch_ok_pushButton.clicked.connect(self.xk_kch_ok_event)
        self.xk_kxh_ok_pushButton.clicked.connect(self.xk_kxh_ok_event)
        self.xk_skls_ok_pushButton.clicked.connect(self.xk_skls_ok_event)
        self.xk_ok_pushButton.clicked.connect(self.xk_ok_event)

    def xk_ok_event(self):
        logs.logs_print("xk_ok_event")
        temp_1 = str(self.enable_xk_data)
        logs.logs_print("可以进行选的课： " + temp_1)

        if temp_1 in self.yjxdk:
            logs.logs_print("选课失败，不能再选这节课了")
            QMessageBox.information(self, '信息', '选课失败，不能再选这节课了', QMessageBox.Yes, QMessageBox.Yes)
            return 0

        if gl.get_value("user_info") is None:
            QMessageBox.information(self, '信息', '请先登录，再查看，或者已经过了选课的时间', QMessageBox.Yes, QMessageBox.Yes)
            return 0

        cookie = gl.get_value("user_info")["cookie"]
        logs.logs_print("cookie： " + cookie)
        res = submit_xk_info_from_free_course_list_element(cookie=cookie,free_course_list_element=temp_1)

        if res != "error":
            self.yjxdk.append(temp_1)
            logs.logs_print("选课成功")
            self.xk_ok_res_label.setText("选课成功")
            self.xk_ok_res_label.setStyleSheet("color:green")
            self.xk_ok_pushButton.setEnabled(False)
            self.xk_kxh_ok_pushButton.setEnabled(False)
            self.xk_skls_ok_pushButton.setEnabled(False)
        else:
            logs.logs_print(res)
            logs.logs_print("选课失败，发生未知错误")
            self.xk_ok_res_label.setText("选课失败，发生未知错误")
            self.xk_ok_res_label.setStyleSheet("color:red")

    def xk_skls_ok_event(self):
        logs.logs_print("xk_skls_ok_event")
        xk_skls = self.xk_skjs_comboBox.currentText()
        logs.logs_print("xk_skls: " + xk_skls)

        skls_list = []
        for i in self.xk_kch_list:
            if "#上课教师:" + xk_skls in i:
                skls_list.append(i)

        if len(skls_list) == 1:
            logs.logs_print("课程选择成功")
            temp_1 = skls_list[0]
            self.xk_kxh_ok_pushButton.setEnabled(False)
            self.xk_skls_ok_pushButton.setEnabled(False)
            self.xk_kcm_label.setText(temp_1.split("程名:")[1].split("#")[0])
            self.xk_kkyx_label.setText(temp_1.split("#开课院系:")[1].split("#")[0])
            self.xk_kslx_label.setText(temp_1.split("#考试类型:")[1].split("#")[0])
            self.xk_jxjhlf_label.setText(temp_1.split("#教学计划年份:")[1].split("#")[0])
            self.xk_kch_res_label.setText("课程选择成功")
            self.xk_kch_res_label.setStyleSheet("color:green")
            self.xk_ok_pushButton.setEnabled(True)
            self.enable_xk_data = skls_list[0]
        elif len(skls_list) > 1:
            logs.logs_print("课程号和老师不能确定一个课程")
            self.xk_kch_res_label.setText("课程号和老师\n不能确定一个课程\n请选择课序号")
            self.xk_kxh_ok_pushButton.setEnabled(True)
            temp_kxh = []
            for i in skls_list:
                temp_kxh.append(i.split('#课序号:')[1].split('#')[0])
            self.xk_kxh_comboBox.clear()
            self.xk_kxh_comboBox.addItems(temp_kxh)
        else:
            logs.logs_print("未知错误")
            self.xk_kch_res_label.setText("未知错误")
            self.xk_kch_res_label.setStyleSheet("color:red")

    def xk_kxh_ok_event(self):
        logs.logs_print("xk_kxh_ok_event")
        xk_kxh = self.xk_kxh_comboBox.currentText()
        logs.logs_print("xk_kxh: " + xk_kxh)
        kxh_list = []
        for i in self.xk_kch_list:
            if "#课序号:" + xk_kxh in i:
                kxh_list.append(i)
        if len(kxh_list) == 1:
            logs.logs_print("课程选择成功")
            temp_1 = kxh_list[0]
            self.xk_kxh_ok_pushButton.setEnabled(False)
            self.xk_skls_ok_pushButton.setEnabled(False)
            self.xk_kcm_label.setText(temp_1.split("程名:")[1].split("#")[0])
            self.xk_kkyx_label.setText(temp_1.split("#开课院系:")[1].split("#")[0])
            self.xk_kslx_label.setText(temp_1.split("#考试类型:")[1].split("#")[0])
            self.xk_jxjhlf_label.setText(temp_1.split("#教学计划年份:")[1].split("#")[0])
            self.xk_kch_res_label.setText("课程选择成功")
            self.xk_kch_res_label.setStyleSheet("color:green")
            self.xk_ok_pushButton.setEnabled(True)
            self.enable_xk_data = kxh_list[0]
        elif len(kxh_list) > 1:
            logs.logs_print("课程号和课序号不能确定一个课程 " + str(kxh_list))
            self.xk_kch_res_label.setText("课程号和课序号\n不能确定一个课程")
            self.xk_kch_res_label.setStyleSheet("color:red")
        else:
            logs.logs_print("未知错误")
            self.xk_kch_res_label.setText("未知错误")
            self.xk_kch_res_label.setStyleSheet("color:red")

    def xk_kch_ok_event(self):
        logs.logs_print("xk_kch_ok_event")
        xk_kch = self.xk_kch_lineEdit.text()

        if gl.get_value("free_course_list") is None:
            self.label_3.setText("请先登录，再查看，或者已经过了选课的时间")
            return 0

        enable_course_list = gl.get_value("free_course_list")

        # TODO 需要基于时间进行开放
        # if not ac_jwc.is_enable_xk():
        #     logs.logs_print("当前为非选课阶段")
        #     self.xk_kch_res_label.setText("当前为非选课阶段")
        #     self.xk_kch_res_label.setStyleSheet("color:red")
        #     return

        for i in enable_course_list:
            if "#课程号:" + xk_kch + "#" in i:
                self.xk_kch_list.append(i)

        if len(self.xk_kch_list) == 0:
            logs.logs_print("课程号填写错误：" + xk_kch)
            self.xk_kch_res_label.setText("课程号填写错误")
            self.xk_kch_res_label.setStyleSheet("color:red")
            self.xk_kxh_ok_pushButton.setEnabled(False)
            self.xk_skls_ok_pushButton.setEnabled(False)
            self.xk_ok_pushButton.setEnabled(False)
            return 0

        xk_kch_list_kxh = []
        for i in self.xk_kch_list:
            xk_kch_list_kxh.append(i.split('#课序号:')[1].split('#')[0])
        self.xk_kxh_comboBox.clear()
        self.xk_kxh_comboBox.addItems(xk_kch_list_kxh)

        xk_kch_list_skls = []
        for i in self.xk_kch_list:
            xk_kch_list_skls.append(i.split('#上课教师:')[1].split('#')[0])
        self.xk_skjs_comboBox.clear()
        self.xk_skjs_comboBox.addItems(list(set(xk_kch_list_skls)))

        self.xk_kxh_ok_pushButton.setEnabled(True)
        self.xk_skls_ok_pushButton.setEnabled(True)
